package com.codeying.controller;

import com.codeying.utils.component.ApiResult;
import com.codeying.mapper.SqlMapper;
import com.codeying.mapper.SqlMapper.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.ui.Model;
import com.codeying.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeying.entity.*;
import org.springframework.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.*;
import java.util.*;
import java.util.Date;

/** 登陆、注册、登出 */
@Controller
public class IndexController extends BaseController {
  @Autowired private SqlMapper sqlMapper;

  @RequestMapping("/")
  public String index() {
    LoginUser user = getCurrentUser();
    if (user == null) return "login";
    return "redirect:hello";
  }

  /** 欢迎页统一入口 */
  @GetMapping("hello")
  public String hello(Model model) {
    return "hello";
  }

  /** 登录页 */
  @GetMapping("login")
  public String login() {
    return "login";
  }

  @PostMapping("login")
  public String login(String captcha, String username, String password, String usertype)
      throws Exception {
    // 校验验证码
    String captchaOrigin = (String) req.getSession().getAttribute("captcha");
    if (captcha == null || !captcha.equalsIgnoreCase(captchaOrigin)) {
      req.setAttribute("message", "验证码错误！");
      return "login";
    }
    // 登录开始
    req.setCharacterEncoding("utf-8");
    // 登陆的用户
    LoginUser loginUser;
    if (usertype.equals("admin")) {
      QueryWrapper<Admin> wrapper = new QueryWrapper<>();
      wrapper.eq("username", username);
      wrapper.eq("password", password);
      loginUser = adminService.getOne(wrapper);
      if (loginUser != null) {
        req.getSession().setAttribute("user", loginUser);
        req.getSession().setAttribute("role", "admin");
        return "redirect:hello";
      }
    }
    if (usertype.equals("user")) {
      QueryWrapper<User> wrapper = new QueryWrapper<>();
      wrapper.eq("username", username);
      wrapper.eq("password", password);
      loginUser = userService.getOne(wrapper);
      if (loginUser != null) {
        req.getSession().setAttribute("user", loginUser);
        req.getSession().setAttribute("role", "user");
        return "redirect:hello";
      }
    }

    req.setAttribute("username", username);
    req.setAttribute("password", password);
    // 登陆失败，就重新登陆
    req.setAttribute("message", "账号密码有误，登陆失败");
    return "login";
  }

  /** 注册 */
  @GetMapping("register")
  public String register() {
    return "register";
  }

  @PostMapping("register")
  public String register(String username, String password, String usertype) throws Exception {
    req.setCharacterEncoding("utf-8");
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
      req.setAttribute("message", "账号密码不可为空！");
      return "register";
    }
    if (usertype.equals("admin")) {
      QueryWrapper<Admin> wrapper = new QueryWrapper<>();
      wrapper.eq("username", username);
      Admin temp = adminService.getOne(wrapper);
      if (temp != null) {
        req.setAttribute("message", "账号已存在！");
        return "register";
      }
      Admin admin = new Admin();
      admin.setUsername(username);
      admin.setPassword(password);
      String id = ""; // 管理员主键
      admin.setId(CommonUtils.newId());
      String name = ""; // 姓名
      String tele = ""; // 电话
      adminService.save(admin);
      req.setAttribute("message", "注册成功，请登陆");
      return "login";
    }
    if (usertype.equals("user")) {
      QueryWrapper<User> wrapper = new QueryWrapper<>();
      wrapper.eq("username", username);
      User temp = userService.getOne(wrapper);
      if (temp != null) {
        req.setAttribute("message", "账号已存在！");
        return "register";
      }
      User user = new User();
      user.setUsername(username);
      user.setPassword(password);
      String id = ""; // 用户主键
      user.setId(CommonUtils.newId());
      String name = ""; // 姓名
      String gender = ""; // 性别
      Integer age = 0; // 年龄
      String tele = ""; // 电话
      userService.save(user);
      req.setAttribute("message", "注册成功，请登陆");
      return "login";
    }
    // 注册失败
    req.setAttribute("message", "请选择角色类型");
    return "register";
  }

  // 登出
  @RequestMapping("logout")
  public String logout() {
    // 让session失效。
    req.getSession().removeAttribute("user");
    return "login";
  }

  // echarts数据
  @PostMapping("hello")
  @ResponseBody
  public ApiResult<List<Echart>> helloData() {
    // 数据
    List<Echart> l = new ArrayList<>();
    {
      // 图表
      Echart echart = new Echart();
      echart.setName("数");
      echart.setType("n"); // line pie
      // echart.setValue(studentService.count()+"");
      // l.add(echart.init());
    }
    {
      // 图表
      Echart echart = new Echart();
      echart.setName("统计");
      echart.setType("bar"); // line pie
      // echart.setDtos(sqlMapper.exec("select id as name,score as value from tb_score"));
      // l.add(echart.init());
    }
    return successData(l);
  }
}

